package cn.tedu.jws.admin.controller;

import cn.tedu.jws.admin.pojo.dto.BuildingAddDto;
import cn.tedu.jws.admin.pojo.dto.BuildingUpdateDTO;
import cn.tedu.jws.admin.pojo.vo.BuildingListVO;
import cn.tedu.jws.admin.service.BuildingService;
import cn.tedu.jws.admin.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @version 0.0.1
 * @Author cn.tedu
 * @Date 2023/03/25/16:37
 */
@Slf4j
@RestController
@Api(tags = "03.楼栋管理模块")
@RequestMapping("/jws/admin/buildings")
public class BuildingController {

    @Autowired
    private BuildingService buildingService;

    @PostMapping("/addNew")
    @ApiOperation("新增房屋信息")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid BuildingAddDto buildingAddDto) {
        buildingService.addNew(buildingAddDto);
        log.debug("添加信息{}", buildingAddDto);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除房间")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "房间数据ID", required = true, dataType = "long"),
    })
    public JsonResult delete(@PathVariable @Range(min = 1, message = "请提交有效值ID!") Long id) {
        log.debug("开始处理【删除数据】的请求，参数：{}", id);
        buildingService.removeById(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改信息")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "数据id", required = true, dataType = "long")
    })
    public JsonResult updateInfoById(@PathVariable @Range(min = 1, message = "请提交有效ID值!") Long id, @Valid BuildingUpdateDTO buildingUpdateDTO) {
        log.debug("开始根据ID:{} 处理【修改相册】的请求，参数：{}", id, buildingUpdateDTO);
        buildingService.updateById(id, buildingUpdateDTO);
        return JsonResult.ok();
    }

    @GetMapping("")
    @ApiOperation("查询楼栋列表")
    @ApiOperationSupport(order = 420)
    public JsonResult list() {
        log.debug("开始查询列表");
        List<BuildingListVO> list = buildingService.list();
        return JsonResult.ok(list);
    }

    @GetMapping("/listByNum")
    @ApiOperation("根据编号查询楼栋列表")
    @ApiOperationSupport(order = 430)
    public JsonResult listByNum(String num) {
        log.debug("开始根据编号查询列表");
        List<BuildingListVO> list = buildingService.listByNum(num);
        return JsonResult.ok(list);
    }

    @GetMapping("/listByDong")
    @ApiOperation("根据楼栋号查询楼栋列表")
    @ApiOperationSupport(order = 440)
    public JsonResult listByDong(Integer dong) {
        log.debug("开始根据楼栋号查询列表");
        List<BuildingListVO> list = buildingService.listByDong(dong);
        return JsonResult.ok(list);
    }
}
